/* This file is part of BonQ - A simple HTTP-based messaging server.
 * Copyright (C) 2009 James Cook
 *
 * BonQ is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License, version 2, as published by the
 * Free Software Foundation.
 *
 * BonQ is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License, version 2,
 * for more details.
 *
 * You should have received a copy of the GNU General Public License, version
 * 2, along with BonQ.  If not, see:
 *
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 */
package com.bonkabonka.queue;

import java.util.LinkedList;

/**
 * This queue retains no data when the server is downed.  If you worry about
 * losing messages, you might look at PersistentQueue.
 * 
 * @author jamesc
 */
public class TransientQueue extends Queue
{
    private final transient LinkedList<String> messages = new LinkedList<String>();

    /**
     * Add a message to the queue.
     * 
     * @param message String containing the message
     * @return null;
     */
    public String push(String message)
    {
        synchronized(messages)
        {
            messages.add(message);
        }

        return null;
    }

    /**
     * Return the next message in the queue, but don't remove it.
     * 
     * @return String containing the message
     */
    public String peek()
    {
        String message = null;

        synchronized(messages)
        {
            if(messages.size() > 0)
            {
                message = messages.get(0);
            }
        }

        return message;
    }

    /**
     * Return the next message in the queue.
     * 
     * @return String containing the message
     */
    public String pull()
    {
        String message = null;

        synchronized(messages)
        {
            if(messages.size() > 0)
            {
                message = messages.remove(0);
            }
        }

        return message;
    }
}
